package com.cg.slms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.cg.slms.domain.Merchandise; 
import com.cg.slms.service.IMerchandiseService;
@Controller
public class MerchandiseController {
	@Resource(name="merchandiseService")
	private IMerchandiseService merchandiseService;
	
	public IMerchandiseService getMerchandiseService() {
		return merchandiseService;
	}
	public void setMerchandiseService(IMerchandiseService merchandiseService) {
		this.merchandiseService = merchandiseService;
	}
	/* 员工档案查询 */
	@RequestMapping("/merchandiseAction")
	public ModelAndView findMerchandise(Merchandise merchandise) {
		ModelAndView modelAndView;
		List<Merchandise> merchandises = merchandiseService.findMerchandise(merchandise);
		if(merchandises!= null){
			modelAndView=new ModelAndView("/archive/merchandise");
			modelAndView.addObject("merchandises", merchandises) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	//添加控制器
	@RequestMapping("/addMerchandiseAction")
	public ModelAndView addMerchandise(@Valid Merchandise merchandise,Errors errors) {
		ModelAndView modelAndView;
		if (errors.getErrorCount()>0){//员工信息出现问题
			modelAndView=new ModelAndView("/archive/addMerchandise");
			return modelAndView;
		}
		boolean ret = merchandiseService.addMerchandise(merchandise);
		if(ret){
			String finish_Url="/slms2/moduls/archive/merchandise.jsp";
			modelAndView=new ModelAndView("forward:/finish.jsp");
			modelAndView.addObject("finish_Url", finish_Url) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");//写入数据库出现问题
		}
		return modelAndView;
	}
	//找到id控制器
	@RequestMapping("/findMerchandiseByIdAction")
	public ModelAndView findMerchandiseById(String id) {
		System.out.print("你好"+id);
		ModelAndView modelAndView;
		Merchandise merchandise= merchandiseService.findMerchandiseById(id);
		if(merchandise!= null){
			modelAndView=new ModelAndView("/archive/updateMerchandise");
			modelAndView.addObject("merchandise",merchandise) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	//修改控制器
	@RequestMapping("/updateMerchandiseAction")
	public ModelAndView updateMerchandise(@Valid Merchandise merchandise,Errors errors) {
		ModelAndView modelAndView;
		if (errors.getErrorCount()>0){
			modelAndView=new ModelAndView("/archive/updateMerchandise");
			return modelAndView;
		}
		boolean ret = merchandiseService.updateMerchandise(merchandise);
		if(ret){
			String finish_Url="/slms2/moduls/archive/merchandise.jsp";
			modelAndView=new ModelAndView("forward:/finish.jsp");
			modelAndView.addObject("finish_Url", finish_Url) ;
		}else{
			modelAndView=new ModelAndView("forward:/error.jsp");
		}
		return modelAndView;
	}
	//删除
	@RequestMapping("/deleteMerchandiseByIdAction")
	public String deleteMerchandiseById(String id) {
		boolean ret= merchandiseService.deleteMerchandiseById(id);
		if(ret){
			return "forward:/merchandiseAction";
		}else{
			return "forward:/error.jsp";
		}
	}
	

}
